import { computed } from 'vue';

export function useHotLabel(ctx) {
  // - [ ] 统计各标签最多出现次数，用于提升排序正确性
  const sortByCount = R.compose( R.pluck(0), R.sort(R.descend( R.prop(1) )), R.toPairs )
  const HotLabelsInPkg = R.compose( sortByCount, R.countBy(R.identity), ctx.LabelsInPkg );

  const hot = computed(() => ctx.appOpts.value.autoLabelSort ? HotLabelsInPkg(ctx.quesList.value) : []);

  /** 题中没选的热门、从没选过的和题已有的标签 */
  const classesSorted = computed(() => {
    if (!ctx.appOpts.value.autoLabelSort) { return ctx.classes.value; }

    const probable = R.difference(hot.value, ctx.LabelsInQues(ctx.ques.value));
    const improbable = R.difference(ctx.classes.value, hot.value);
    return R.compose(R.uniq, R.unnest)([ probable, improbable, ctx.LabelsInQues(ctx.ques.value) ]);
  });

  return classesSorted;
}
